<?php

namespace App\Home\Controllers;

use Illuminate\Support\Arr;
use Overtrue\Socialite\User as SocialiteUser;
use Illuminate\Http\Request;
use Log;

class WeChatController extends Controller
{
    public function __construct(){
        $this->app = app('wechat.official_account');
    }

    /**
     * 处理微信的请求消息
     */
    public function serve()
    {
        # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志
        Log::info('request arrived.'); 
        $this->app->server->push(function($message){
            return "欢迎关注";
        });Artisan::call('config:cache');
        return $this->app->server->serve();
    }

    public function jssdkBuildConfig(Request $request)
    {
        $request->validate([
            'url' => 'required|string',
        ]);
        $APIs = [
            'chooseImage',
            'uploadImage',
            'downloadImage',
            'previewImage',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'getLocation',
            'openLocation'
        ];
        $this->app->jssdk->setUrl($request->input('url'));
        $data = json_decode($this->app->jssdk->buildConfig($APIs),true);
        return $this->ok($data);
    }

    public function user()
    {
        return $this->ok(session()->get('users'));
    }

    /**
     * 发送模版消息
     */
    public function sendTemplateMessag()
    {
        $re = $this->app->template_message->send([
            'touser' => 'ojDTV6IPFSEKZ8E4xRcqNBtbqtDg',
            'template_id' => 'wGXcjuoqn1olJu_015xN7KDAgqqFXcDTitjC6LgH0Ik',
            'url' => 'https://easywechat.org',
            'data' => [
                'first' => 'first',
                'keyword1' => 'keyword1',
                'keyword2' => 'keyword2',
                'remark' => 'remark',
            ],
        ]);
        return $this->ok($re);
    }
}